package com.arron.algorithm.leetcodetop100.bfs;

import com.arron.algorithm.tree.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 *  dfs 版本
 * @see com.arron.algorithm.leetcodetop100.二叉树.二叉树的右视图
 */
public class 二叉树的右视图 {

    public List<Integer> rightSideView(TreeNode root) {


        if (root == null){
            return new ArrayList<>();
        }

        Queue<TreeNode> queue = new LinkedList<>();


        List<Integer> res = new ArrayList<>();
        queue.offer(root);

        while (!queue.isEmpty()){
            int n = queue.size();
            for (int i = 0; i < n; i++) {
                TreeNode poll = queue.poll();
                if (i == n-1){
                    res.add(poll.val);
                }

                if (poll.left!=null){
                    queue.offer(poll.left);
                }
                if (poll.right!=null){
                    queue.offer(poll.right);
                }
            }
        }

        return res;
    }


}
